package ch.subgate.spritestudio.types;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import ch.subgate.spritestudio.constants.Constants;
import ch.subgate.spritestudio.tools.Converter;

import android.os.Environment;

public class Palette {
	private String mName;
	
	private ArrayList<Integer> mColors;
	
	public Palette(String name, ArrayList<Integer> colors) {
		mName = name;
		mColors = colors;
	}
	
	public String getName() {
		return mName;
	}
	
	public ArrayList<Integer> getColors() {
		return mColors;
	}
	
	public void addColor() {
		mColors.add(Integer.valueOf(0xff000000));
	}
	
	public void addColor(int color) {
		mColors.add(Integer.valueOf(color));
	}
	
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		if (mColors != null) {
			mColors.clear();
			mColors = null;
		}
	}
	
	@Override
	public String toString() {
		return mName;
	}
	
	public static void savePalette(Palette palette) {
		File file = new File(
				Environment.getExternalStorageDirectory()
				+ "/" + Constants.DEFAULT_MAIN_DIR
				+ "/" + Constants.DEFAULT_PALETTE_DIR
				+ "/" + palette.getName() + ".ssp");
		
		if (file.exists()) {
			file.delete();
		}
		
		OutputStream out = null;
		
		try {
			out = new BufferedOutputStream(new FileOutputStream(file));
			
			// Write color count
			out.write(palette.getColors().size());
			
			// Write palette colors
			for (Integer color : palette.getColors()) {
				out.write(Converter.getByteArrayFromInteger(color.intValue()));
			}
		}
		catch (Exception e) {
			// TODO
		}
		finally {
			try {
				if (out != null) {
					out.close();
				}
			}
			catch (IOException e) {
				// TODO
			}
		}
	}
	
	public static void loadPalettes(ArrayList<Palette> palettes) {
		File pdir = new File(
				Environment.getExternalStorageDirectory()
				+ "/" + Constants.DEFAULT_MAIN_DIR
				+ "/" + Constants.DEFAULT_PALETTE_DIR);
		
		File[] files = pdir.listFiles();
		Palette palette;
		
		if (palettes != null && files != null) {
			for (File file : files) {
				if (file.isFile()) {
					palette = loadPalette(file);
					if (palette != null) {
						palettes.add(palette);
					}
				}
			}
		}
	}
	
	public static Palette loadPalette(String name) {
		File file = new File(
				Environment.getExternalStorageDirectory()
				+ "/" + Constants.DEFAULT_MAIN_DIR
				+ "/" + Constants.DEFAULT_PALETTE_DIR
				+ "/" + name + ".ssp");
		return loadPalette(file);
	}
	
	public static Palette loadPalette(File file) {
		Palette palette = null;
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(file));
			int colorCount = in.read();
			if (colorCount >= 0) {
				ArrayList<Integer> colors = new ArrayList<Integer>();
				byte[] buffer = new byte[4];
				while (in.read(buffer) == buffer.length) {
					colors.add(Converter.getIntegerFromByteArray(buffer));
				}
				return new Palette(
						cutFileEnding(file.getName()),
						colors);
			}
			else {
				return null;
			}
		}
		catch (Exception e) {
			// TODO
		}
		finally {
			try {
				if (in != null) {
					in.close();
				}
			}
			catch (IOException e) {
				// TODO nothing
			}
		}
		
		return palette;
	}
	
	public static String cutFileEnding(String fileName) {
		if (fileName.endsWith(".ssp")) {
			return fileName.substring(0, fileName.length() - 4);
		}
		return fileName;
	}
}
